//
//  SearchViewController.m
//  Manager
//
//  Created by 彭凯敏 on 15/9/1.
//  Copyright (c) 2015年 52xiaoluo. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchCell.h"

@interface SearchViewController ()<UISearchBarDelegate>

@property (strong, nonatomic) UISearchBar *searchBar;

@property (strong, nonatomic) UITableView *searchTableView;
@property (strong, nonatomic) NSMutableArray *searchDataArray;


@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestSuccess:) name:CMD_ViewRoom_RoomSerachReq object:nil];
    

    
    [self createUI];
    
    _searchDataArray = [[NSMutableArray alloc]init];
    
    
    [_searchTableView reloadDataAndisNil:0 nitText:@"无房源信息"];
    _searchBar.text = _searchStr;
    [_searchBar becomeFirstResponder];
    [self RoomSearchReqWithtext:_searchStr];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [self viewEndEditing];
    
}

- (void)createUI
{
    UIBarButtonItem *leftNavgationItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:nil];
    leftNavgationItem.imageInsets = UIEdgeInsetsMake(0, -8, 0, 8);
    
    leftNavgationItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = leftNavgationItem;
    
    [leftNavgationItem setAction:@selector(backVC)];
    UITapGestureRecognizer *tap0 = [[UITapGestureRecognizer alloc]initWithTarget:self  action:@selector(viewEndEditing)];
    [self.navigationController.navigationBar addGestureRecognizer:tap0];
    
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    _searchBar.delegate = self;
    _searchBar.placeholder = @"小区/房间号";
    _searchBar.keyboardType =  UIKeyboardTypeDefault;
    [_searchBar setTranslucent:YES];
    
    self.navigationItem.titleView = _searchBar;
    
    DLog(@"%f",self.navigationItem.titleView.originX);
    
    self.navigationItem.titleView.originX = self.navigationItem.titleView.originX-20;
    _searchBar.originX = -20;
    DLog(@"%f",self.navigationItem.titleView.originX);
    
    _searchTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    [_searchTableView setLineType:TableViewLineTypeAll];
    _searchTableView.delegate = self;
    _searchTableView.dataSource = self;
    
    
    [self.view addSubview:_searchTableView];
    
    
    
}


- (void)RoomSearchReqWithtext:(NSString *)text
{
    RoomSearchReqBuilder *builder = [[RoomSearchReqBuilder alloc]init];
    RoomFilterBuilder *filter = [[RoomFilterBuilder alloc]init];
    [filter setName:text];
    
    [builder setRoomFilter:filter.build];
    
    [[ViewRoomLogic shareInstance]roomSearchReqWithBuild:builder.build];
    
}
- (void)requestSuccess:(NSNotification *)noti
{
    dispatch_async(dispatch_get_main_queue(), ^{

        if ([noti.name isEqualToString: CMD_ViewRoom_RoomSerachReq]) {
            if ([noti.object isKindOfClass:[ErrorModel class]])
            {
                [_searchTableView reloadDataAndisNil:0 nitText:@"房源搜索错误"];
                return ;
            }
            [_searchDataArray setArray:noti.object];
            [_searchTableView reloadDataAndisNil:_searchDataArray.count nitText:@"无房源信息"];

        }
        
    });
}
#pragma mark - events

- (void)viewEndEditing
{
    if ([_searchBar isFirstResponder]) {
        [_searchBar resignFirstResponder];
    }
    
    
}
- (void)backVC
{
    [self viewEndEditing];
    if (_searchDataArray.count == 0 || [_searchBar.text isEqualToString:@""]) {
        if (_delegate && [_delegate respondsToSelector:@selector(searchNoRoom)]) {
            [_delegate searchNoRoom];
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self RoomSearchReqWithtext:searchText];
    
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    
    return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self RoomSearchReqWithtext:searchBar.text];
    [self viewEndEditing];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self viewEndEditing];
    
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self viewEndEditing];
    
    RoomInfo *info = _searchDataArray[indexPath.row];
    if (_delegate && [_delegate respondsToSelector:@selector(searchSelectRoom:)]) {
        [_delegate searchSelectRoom:info];
        
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _searchDataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SearchCell";
    SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"SearchCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    }
    
    RoomInfo *info = _searchDataArray[indexPath.row];
    
    cell.roomInfo = info;
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
    
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
#ifdef __IPHONE_8_0
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
#endif
    
    
}


@end
